// 2013-7-28上午11:22:05  net.betou.trade.main.dao.impl.IntermediateProvidersAndAdministratorsDaoImpl.java

package net.betou.trade.main.dao.impl;

import net.betou.common.hibernate3.Finder;
import net.betou.common.hibernate3.HibernateBaseDao;
import net.betou.common.page.Pagination;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.main.dao.AgentAndAdminsDao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

/**
 * 居间、居间管理员、系统管理员DAO
 * @author 叶装装
 */
@Repository
public class AgentAndAdminsDaoImpl extends HibernateBaseDao< AgentAndAdmins, Integer > implements AgentAndAdminsDao{

	public int countByEmail( String email ) {
		return count( "email", email );
	}

	public int countByMobile( String mobile ) {
		return count( "mobile", mobile );
	}

	public int countByName( String name ) {
		return count( "intermediaName", name );
	}

	public int countByUsername( String username ) {
		return count( "username", username );
	}

	public AgentAndAdmins delete( AgentAndAdmins bean ) {
		if( bean != null ){
			getSession( ).delete( bean );
		}
		
		return bean;
	}
	
	public AgentAndAdmins deleteById( Integer id ){
		AgentAndAdmins bean = get( id );
		if( bean != null ){
			getSession( ).delete( bean );
		}
		
		return bean;
	}

	public AgentAndAdmins findById( Integer id ) {
		AgentAndAdmins bean = get( id );
		return bean;
	}
	
	public AgentAndAdmins findMyData( Integer dataId, Integer parentId, Boolean justAdmin ){
		String hql = "from AgentAndAdmins bean where bean.id=? and bean.parent.id=? and bean.system=? and bean.justAdmin=?";
		return ( AgentAndAdmins )findUnique( hql, dataId, parentId, false, justAdmin );
	}
	
	 public AgentAndAdmins findAgentOrAdmins( Boolean isSystem, Boolean justAdmin,  Integer id ){
		 String hql = "from AgentAndAdmins bean where bean.id=? and bean.system=? and bean.justAdmin=?";
		 
		 return ( AgentAndAdmins )findUnique( hql, id, isSystem, justAdmin );
	 }

	public AgentAndAdmins findByUsername( String username ) {
		return findUniqueByProperty( "username", username );
	}

	public AgentAndAdmins save( AgentAndAdmins bean ) {
		getSession( ).save( bean );
		return bean;
	}
	
	private int count( String name, String value ){
		String hql = "select count(*) from AgentAndAdmins bean where bean." + name + "=:" + name;
		Query query = getSession( ).createQuery( hql );
		query.setParameter( name, value );
		return ( ( Number ) query.iterate( ).next( ) ).intValue( );
	}
	
	 public Pagination getPage( Boolean system, Boolean justAdmin, Boolean agencyOrProxy,  Integer selfId, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize, String filterStatus ){
		
		 Finder f = Finder.create("select bean from AgentAndAdmins bean where 1=1 ");
		 
		 if( system != null ){
			 f.append( " and bean.system=:system " );
			 f.setParam( "system", system );
		 }
		 
		 if( justAdmin != null ){
			 f.append( " and bean.justAdmin=:justAdmin " );
			 f.setParam( "justAdmin", justAdmin );
		 }
		 
		 if( agencyOrProxy != null ){
			 f.append( " and bean.agencyOrProxy=:agencyOrProxy" );
			 f.setParam( "agencyOrProxy", agencyOrProxy );
		 }
		 
		 if( selfId != null ){
			 f.append( " and bean.parent.id =:selfId " );
			 f.setParam( "selfId", selfId );
		 }
		 
		createFilter( f, filterStatus );
		createSearch( f, searchProperty, searchValue );
		createOrder( f, orderProperty, orderDirection, "id" );
		
		return find(f, pageNo, pageSize);
	}
	
	@Override
	protected Class< AgentAndAdmins > getEntityClass( ) {
		return AgentAndAdmins.class;
	}
}
